package com.milhouz.bomberrokon.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.milhouz.bomberrokon.BomberSlashActivity;
import com.milhouz.bomberrokon.R;
import com.milhouz.bomberrokon.ResultRoundActivity;
import com.milhouz.bomberrokon.model.GameRound;
import com.milhouz.bomberrokon.model.MapModel;
import com.stickycoding.Rokon.Rokon;

public class EndRoundDialog extends Dialog implements OnClickListener {

	private GameRound mGameRound;
	private MapModel mMapModel;

	private TextView mEndRoundText;
	private Button mCloseButton;

	public EndRoundDialog(Context context, GameRound gameRound, MapModel mapModel) {
		super(context);
		mGameRound = gameRound;
		mMapModel = mapModel;
	}

	@Override
	public void show() {
		super.show();
		mGameRound.setEndRoundShowed(true);
		Rokon.getRokon().pause();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.end_round_dialog);

		// Set Round text
		mEndRoundText = (TextView) findViewById(R.id.endRoundText);
		if (!mGameRound.isPlayerLost()) {
			mEndRoundText.setText("You win :D\n\n");
		} else {
			mEndRoundText.setText("You lose :(\n\n");
		}

		// Set close button
		mCloseButton = (Button) findViewById(R.id.closeButton);
		mCloseButton.setOnClickListener(this);
		if (mGameRound.isFinishedParty()) {
			// Show button Go to Result Activity
			mCloseButton.setText("Finish !");
		} else {
			// Next round
			mCloseButton.setText("Next Round !");
		}
	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.closeButton:
			dismiss();
			if (mGameRound.isFinishedParty()) {
				// Go to result activity
				mGameRound.finishRound();
				Intent i = new Intent(getContext(), ResultRoundActivity.class);
				i.putExtra("opponents", mGameRound.getNbPlayers() - 1);
				for (int bomberId = 0; bomberId < mGameRound.getNbPlayers(); bomberId++) {
					i.putExtra("PLAYER" + bomberId, mGameRound.getVictory(bomberId));
				}
				getContext().startActivity(i);
			} else {
				// Go to next round
				mGameRound.finishRound();
				Intent i = new Intent(getContext(), BomberSlashActivity.class);
				i.putExtra("mapCode", mMapModel.getMapCode());
				i.putExtra("opponents", mGameRound.getNbPlayers() - 1);
				i.putExtra("timer", mGameRound.getTimer().getTimerOriginalValue());
				i.putExtra("currentRound", mGameRound.getCurrentRound());
				for (int bomberId = 0; bomberId < mGameRound.getNbPlayers(); bomberId++) {
					i.putExtra("PLAYER" + bomberId, mGameRound.getVictory(bomberId));
				}
				getContext().startActivity(i);
			}
			break;
		}
	}
}
